use TestHelper;
use Entity;

sub testType{
    my $obj = shift;
    my $type = shift;
    

    print "\n";
    print "Comprobando tipo: $type\n";
    print ">Tipo concreto\n";
    if( (ref $obj) ne $type){
	print "    Tipo del objeto no valido: ".(ref $obj)."\n";
    }else{
	print "    Tipo correcto\n";
    }

    print ">Tipo\n";
    if($obj->type ne $type){
	print "    Tipo del objeto no valido: ".($obj->type)."\n";
    }else{
	print "    Tipo correcto\n";
    }
} # testType;

sub testFields{
    my $obj = shift;
    my ($expectedRef, $hashesRef, $scalarsRef, $arraysRef) = (shift, shift, shift, shift);

    my @expectedKeys = ();
    my @hashKeys     = ();
    my @scalarKeys   = ();
    my @arrayKeys    = ();

    @expectedKeys = @{$expectedRef} if(defined $expectedRef);
    @hashKeys     = @{$hashesRef  } if(defined $hashesRef  );
    @scalarKeys   = @{$scalarsRef } if(defined $scalarsRef );
    @arrayKeys    = @{$arraysRef  } if(defined $arraysRef  );

    
    print "\n";
    print "Probando Campos\n";

    foreach(@expectedKeys){
	if (!(exists $obj->{$_}) || !defined $obj->{$_}){
	    print "--->ERROR: Campo no definido! : ".$_."\n";
	}else{
	    print "    $_ OK\n";
	}
    }

    print "\n";
    print "Comprobando tipo de atributos\n";

    print ">Hashes\n";
    foreach(@hashKeys){
	if ( ref($obj->{$_}) eq '' || ref($obj->{$_}) ne 'HASH'){
	    print "--->ERROR: $_ no es un hash! - ".(ref($obj->{$_}))."\n";
	}else{
	    print "    $_ OK\n";
	}
    }

    print ">Escalares\n";
    foreach(@scalarKeys){
	if ( ref($obj->{$_}) ne ''){
	    print "--->ERROR: $_ no es un escalar! - ".(ref($obj->{$_}))."\n";
	}else{
	    print "    $_ OK\n";
	}
    }

    print ">Arrays\n";
    foreach(@arrayKeys){
	if ( ref($obj->{$_}) ne 'ARRAY'){
	    print "--->ERROR: $_ no es un array! - ".(ref($obj->{$_}))."\n";
	}else{
	    print "    $_ OK\n";
	}
    }
  
} # testFields

sub testAttributes{
    my $obj = shift;
    my ($name, $val) = (shift, shift);

    print "\n";
    print "Probando atributos\n";

    $attr = $obj->attr($name, $val);    
    if($attr != $val){
	print "--->ERROR: Atributo $name no es $val\n";
    }else{
	print "    $name=$val OK\n";
    }

    $attr = $obj->attr($name);
    if($attr != $val){
	print "--->ERROR: Atributo $name no es $val\n";
    }else{
	print "    $name=$val OK\n";
    }
} # testAttributes

sub testPic{
    my $obj = shift;
    my $pic = shift;

    print "\n";
    print "Probando imagen\n";

    if($obj->pic ne "$pic"){
	print "--->ERROR: Imagen incorrecta: ".$obj->pic." ne $pic \n";
    }else{
	print "    ".$obj->pic." OK\n";
    }
} # testPic


sub testContents{
    my $obj = shift;

    print "\n";
    print "Probando Contenidos\n";
    
    $contents = $obj->contents;

    if(ref $contents ne "EntityBag"){
	print "--->ERROR: Tipo de la coleccion de contenidos incorrecto: ".ref $contents." ne EntityBag \n";
    }else{
	print "    Tipo OK\n";
    }

    if($contents ne $obj->contents){
	print "--->ERROR: No son el mismo objeto: $contents ne ".$obj->contents."\n";
    }else{
	print "    Identidad OK\n";
    }

} # testContents

sub testPos{
    $obj = shift;

    print "\n";
    print "Probando Posicion\n";

    $pos = $obj->pos;
    
    if($pos ne $obj->pos){
	print "--->ERROR: No son el mismo objeto: $pos ne ".$obj->pos."\n";
    }else{
	print "    Identidad OK\n";
    }

}
1;
